// 用户相关存储
import { defineStore } from 'pinia'
import { reqLogin } from '@/api/user/index'
import type { logingRquData, logingResData } from '@/api/user/type'
import type { userState } from './types/type'
import { SET_TOKEN, GET_TOKEN } from '@/utils/token'
const useUserStore = defineStore('User', {
  state: (): userState => {
    return {
      // 存储token
      token: GET_TOKEN(),
    }
  },
  actions: {
    async userLogin(data: logingRquData) {
      const result: logingResData = await reqLogin(data)
      if (result.code == 200) {
        // 类型断言，只有当token为字符串的时候才赋值
        this.token = result.data.token as string
        // 本地存储持久化
        SET_TOKEN(result.data.token as string)
        return 'success'
      }
      return Promise.reject(new Error(result.data.message))
    },
  },
  getters: {},
})
export default useUserStore
